package org.orz.story.works.biz.clubhouse.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.orz.cloud.auth.UserUtil;
import org.orz.cloud.auth.domain.UserInfo;
import org.orz.cloud.common.enums.AliveFlagEnum;
import org.orz.story.works.biz.clubhouse.model.req.AddNoteReq;
import org.orz.story.works.biz.clubhouse.model.resp.ListNoteResp;
import org.orz.story.works.biz.clubhouse.persistence.entities.WorksNote;
import org.orz.story.works.biz.clubhouse.persistence.mappers.WorksNoteMapper;
import org.orz.story.works.biz.clubhouse.service.WorksNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WorksNoteServiceImpl implements WorksNoteService {
    @Autowired
    private WorksNoteMapper worksNoteMapper;


    @Override
    public void addNote(AddNoteReq req) {
        UserInfo userInfo = UserUtil.getUserInfoWithExistCheck();
        WorksNote worksNote = new WorksNote();
        worksNote.setContent(req.getContent());
        worksNote.setAccountId(userInfo.getAccountId());
        worksNote.setCreateUser(userInfo.getAccountId());
        worksNote.setCreateDate(new Date());
        worksNote.setUpdateUser(userInfo.getAccountId());
        worksNote.setUpdateDate(new Date());
        worksNoteMapper.insert(worksNote);
    }

    @Override
    public List<ListNoteResp> listNote() {
        List<WorksNote> worksNotes = worksNoteMapper.selectList(new LambdaQueryWrapper<WorksNote>()
                .eq(WorksNote::getAliveFlag, AliveFlagEnum.ALIVE.getKey())
                .orderByDesc(WorksNote::getId));
        if(CollUtil.isEmpty(worksNotes)){
            return Collections.emptyList();
        }

        return worksNotes.stream().map(item -> {
            ListNoteResp resultData = new ListNoteResp();
            resultData.setContent(item.getContent());
            resultData.setCreateDate(item.getCreateDate());
            return resultData;
        }).collect(Collectors.toList());
    }
}
